﻿app.controller('SpecDicCtrl', function ($scope, $modal, $log, $http, $interval, $timeout, $element, tabService, menuService/*, dicSystem, dicData*/) {
    $scope.myTable = {};
    var selected = new Array();
    var dtOrder;
    $scope.open = function (type, size) {
        if (type == 'edit' && (selected.length > 1 || selected.length == 0)) {
            angular.alert('请选中一个项目进行编辑！');
            return;
        }
        dtOrder = $scope.myTable.api().order();
        var modalInstance = $modal.open({
            templateUrl: 'template/dictsystematicadd.html',
            controller: 'DicSystemAddCtrl',
            size: 'lg',
            backdrop: 'static',
            resolve: {
                type: function () {
                    return type;
                },
                selected: function () {
                    return selected;
                },
                deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load(['js/services/dicSet.js',
                                                 'js/controllers/dictsystematicadd.js']);
                    }]
            }
        });

        modalInstance.result.then(function (item) {
            switch (type) {
                case 'edit':
                    dicSystem.Edit(item).success(function (data, header, config, status) {
                        //dtReload();
                        //alert('success');
                        init();
                    }).error(function (data, header, config, status) {
                        angular.alert('操作失败！');
                    });
                    break;
                default:
                    dicSystem.Add(item).success(function (data, header, config, status) {
                        //item.SerialNo = data;
                        //dtReload(OperationMode.Add, item);
                        //alert('success');
                        init();
                        if (dtOrder) {
                            $scope.myTable.api().order(dtOrder).draw();
                            dtOrder = null;
                        }
                    }).error(function (data, header, config, status) {
                        angular.alert('操作失败！');
                    });
                    break;
            }
            $log.info('OK at: ' + new Date());
        }, function (item) {
            if (item) {
                init();
            }
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    if (typeof OperationMode == 'undefined') {
        var OperationMode = {
            Search: 0,
            Add: 1,
            Edit: 2,
            Delete: 3
        }
    }

    var dtReload = function (operationMode, item) {
        var existing = $scope.dtData.slice();
        switch (operationMode) {
            case OperationMode.Add:
                if (item) {
                    existing.push(item);
                }
                break;
            case OperationMode.Delete:
                if (item) {
                    existing.splice($.inArray(item, existing), 1);
                }
                break;
            default:
                break;
        }
        $scope.dtData = existing;
    };

    $scope.remove = function () {
        if (selected.length == 0) {
            angular.alert('请选中项目后再删除！');
            return;
        }
        angular.alert('确定删除选中的项目吗？', null, true).ok(function () {
            dicSystem.Delete(selected[0].SerialNo).success(function (data, header, config, status) {
                //dtReload(OperationMode.Delete, selected[0]);
                //alert('success');
                init();
            }).error(function (data, header, config, status) {
                angular.alert('操作失败！');
            });
        });
    }

    //var a = $('#table1').dataTable().fnSettings();

    $scope.rowCallback = function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        angular.element(nRow).unbind('click');
        angular.element(nRow).bind('click', function () {
            $scope.someClickHandler(angular.element(nRow), aData);
        });

        angular.element(nRow).unbind('dblclick');
        angular.element(nRow).bind('dblclick', function () {
            if (aData.Key == null) {
                angular.alert('找不到对应的字典系统！');
                return;
            }
            //dicData.GetTree(aData.Key).success(function (data, header, config, status) {
            //    if (data) {
                    var strkey = "app.icddictionarytree";
                    var isExistTab = tabService.getTabByOperID(aData.Key);
                    if (isExistTab == null) {
                        var selectTab = tabService.getTabByKey(strkey);
                        var selectMenu = menuService.getMenuByKey(strkey);
                        var templateurlstr = menuService.GetTemplateUrlString(strkey);
                        var params = {}
                        if (aData.Key) {
                            params.key = aData.Key;
                            switch (aData.Key) {
                                case 'e0ab32d0-a16f-11e6-8343-00ff72c03ab3':
                                    params.type = 'ICD-10';
                                    break;
                                case 'e0ab32d1-a16f-11e6-8343-00ff72c03ab3':
                                    params.type = 'ICD-9';
                                    break;
                            }
                        }
                        var item1 = new tabService.tabItem(strkey, aData.Value, true, true, selectMenu.id, selectTab, true, templateurlstr, aData.Key, params);
                        if (!tabService.addTab(item1)) {
                            angular.alert('最多只能添加10个菜单！');
                        }
                    } else {
                        tabService.selectTabByID(isExistTab.id);
                    }
            //    } else {
            //        angular.alert('该数据有误！');
            //        return;
            //    }
            //}).error(function (data, header, config, status) {
            //    //处理响应失败
            //    angular.alert('API接口响应错误！');
            //})
        });
        return nRow;
    };

    $scope.someClickHandler = function (row, info) {
        if (row.hasClass('selected')) {
            selected.splice($.inArray(info, selected), 1);
        } else {
            selected.length = 0;
            selected.push(info);
            row.parent().find(".selected").toggleClass('selected');
        }
        row.toggleClass('selected');
    };

    $scope.columnDefs = [
        { data: 'SerialNo', orderable: false, searchable: false, title: '序号', type: 'num', visible: true,/* width: '10%',*/ targets: 0 },
        //{ data: null, "targets": 0 },
        { data: 'Value', targets: 1 },
        { data: 'AbbInfo.Name', targets: 2 },
        { data: 'StandardCode', targets: 3 },
        { data: 'DimensionInfo.Value', targets: 4 },
        { data: 'RecommendFlag', targets: 5 },
        { data: 'Version', targets: 6 }
    ];

    $scope.dtOptions = {
        order: [[0, 'desc']],
        info: true,
        ordering: true,
        paging: true,
        processing: false,
        searching: true,
        scrollY: '100px',
        autoWidth: false,
        stateSave: false,
        stateDuration: 60 * 60 * 24,
        lengthMenu: [14, 50, 100],
        pagingType: 'full_numbers',
        search: {
            caseInsensitive: false,
            regex: true,
            smart: false
        },
        language: {
            url: 'vendor/jquery/datatables/Chinese.json'
        },
        drawCallback: function () {
            var selectedTab = tabService.getTabByMenuID();
            var contentElement = angular.element("#" + "myMenu" + selectedTab.id).find(".contentElement");
            setAutoTableHeadClass(contentElement);

            if (this.api().column(0).nodes() == null) {
                return;
            }
            this.api().column(0).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }
    };

    //$scope.dtData = '{}';
    var init = function () {
        var icd10 = {
            DimensionInfo: {
                Key: "2350ee62-a174-11e6-8343-00ff72c03ab3",
                Code: "t_00002",
                Value: "国际标准",
                EnglishName: null,
                Description: null,
                InputCode: null,
                AbbInfo: {
                    Code: null,
                    Name: "国际标准"
                },
                StatusCode: "1",
                FromMethod: null,
                SerialNo: 2
            },
            RecommendFlag: 0,
            StandardCode: "ICD-10",
            SpecialFlag: 0,
            SpecialTableName: null,
            SourceMethod: null,
            IssueDate: new Date("2003-07-25 00:00:00"),
            ImpDate: new Date("2003-12-01 00:00:00"),
            Version: null,
            Key: "e0ab32d0-a16f-11e6-8343-00ff72c03ab3",
            Code: "ts_9999",
            Value: "ICD-10 疾病与分类代码",
            EnglishName: null,
            Description: null,
            InputCode: null,
            AbbInfo: {
                Code: null,
                Name: "ICD-10"
            },
            StatusCode: null,
            FromMethod: null,
            SerialNo: 1
        }
        var icd9 = {
            DimensionInfo: {
                Key: "2350ee62-a174-11e6-8343-00ff72c03ab3",
                Code: "t_00002",
                Value: "国际标准",
                EnglishName: null,
                Description: null,
                InputCode: null,
                AbbInfo: {
                    Code: null,
                    Name: "国际标准"
                },
                StatusCode: "1",
                FromMethod: null,
                SerialNo: 2
            },
            RecommendFlag: 0,
            StandardCode: "ICD-9",
            SpecialFlag: 0,
            SpecialTableName: null,
            SourceMethod: null,
            IssueDate: new Date("2003-07-25 00:00:00"),
            ImpDate: new Date("2003-12-01 00:00:00"),
            Version: null,
            Key: "e0ab32d1-a16f-11e6-8343-00ff72c03ab3",
            Code: "ts_9998",
            Value: "ICD-9 疾病与分类代码",
            EnglishName: null,
            Description: null,
            InputCode: null,
            AbbInfo: {
                Code: null,
                Name: "ICD-9"
            },
            StatusCode: null,
            FromMethod: null,
            SerialNo: 1
        }
        var a = [icd10, icd9];
        $scope.dtData = a;

        //dicSystem.GetAll().success(function (data, header, config, status) {
        //    //响应成功
        //    $scope.dtData = data;
        //}).error(function (data, header, config, status) {
        //    //处理响应失败
        //    angular.alert('操作失败！');
        //    $log.log(status)
        //}).finally(function () {
        //    hideThead();
        //})
    };

    function hideThead() {
        $timeout(function () {
            var selectedTab = tabService.getTabByMenuID();
            var contentElement = angular.element("#" + "myMenu" + selectedTab.id).find(".contentElement");
            setAutoTableHead(contentElement);
            setAutoTableHeight(contentElement);
        }, 500)
        //var timeMethod = $interval(function () {
        //    var selectedTab = tabService.getTabByMenuID();
        //    var contentElement = angular.element("#" + "myMenu" + selectedTab.id).find(".contentElement");
        //    setAutoTableHead(contentElement);
        //    setAutoTableHeight(contentElement);
        //}, 1000);
        //$scope.$on('$destroy', function () {
        //    $interval.cancel(timeMethod);
        //});
    }

    init();
});
